//
//  GQHReviewController.m
//  Xanthium
//
//  Created by GuanQinghao on 05/03/2018.
//  Copyright © 2018 GuanQinghao. All rights reserved.
//

#pragma mark Macro
#import "GQHHeader.h"

#pragma mark Model
#import "GQHCommentModel.h"

#pragma mark Object

#pragma mark View
#import "GQHReviewView.h"

#pragma mark Controller
#import "GQHReviewController.h"

#pragma mark CocoaPods

#pragma mark ----------Header End----------

@interface GQHReviewController () <GQHReviewViewDelegate> {
    
#pragma mark Data
    /// 评论模型数组
    NSMutableArray *commentModelArray;
#pragma mark View
    /// 自定义根视图
    GQHReviewView *rootView;
}

@end


@implementation GQHReviewController

#pragma mark --Lifecycle
- (void)loadView {
    DLog();
    
    rootView = [[GQHReviewView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    rootView.qh_delegate = self;
    self.view = rootView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    DLog();
    
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters setObject:USER_GET_ID forKey:@"byEvaluation"];
    
    [self fetchCommentsWithParameters:parameters];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    DLog();
    
    self.qh_titleLabel.text = @"我的评价";
    self.qh_rightButton.hidden = YES;
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    [rootView.qh_tableView.mj_header beginRefreshing];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    DLog();
    
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    
    [rootView.qh_tableView.mj_header endRefreshing];
}

#pragma mark --Delegate
- (void)qh_checkReview {
    DLog(@"查看我评价的");
    
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters setObject:USER_GET_ID forKey:@"evaluationPeople"];
    
    [self fetchCommentsWithParameters:parameters];
    [rootView.qh_tableView.mj_header beginRefreshing];
}

- (void)qh_checkComment {
    DLog(@"查看评价我的");
    
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters setObject:USER_GET_ID forKey:@"byEvaluation"];
    
    [self fetchCommentsWithParameters:parameters];
    [rootView.qh_tableView.mj_header beginRefreshing];
}

- (void)qh_removeReview {
    DLog(@"删除我评价的");
    
    
}

#pragma mark --TargetMethod

#pragma mark --PrivateMethod
- (void)fetchCommentsWithParameters:(NSMutableDictionary *)parameters {
    
    __block int page = 1;
    [parameters setObject:ROWS forKey:@"rows"];
    
    rootView.qh_tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        page = 1;
        [parameters setObject:[NSNumber numberWithInt:page] forKey:@"page"];
        
        [GQHCommentModel qh_fetchCommentsWithParameters:parameters success:^(BOOL status, NSNumber *code, NSString *message, NSArray<GQHCommentModel *> *comments) {
            
            if (status) {
                
                commentModelArray = [NSMutableArray arrayWithArray:comments];
                rootView.qh_commentModelArray = commentModelArray;
                
                /// 按钮上的评价数
//                NSString *reviewTitle = [NSString stringWithFormat:@"我评价的(%lu)",(unsigned long)commentModelArray.count];
//                [rootView.qh_reviewButton setTitle:reviewTitle forState:UIControlStateNormal];
//                NSString *commentTitle = [NSString stringWithFormat:@"我评价的(%lu)",(unsigned long)commentModelArray.count];
//                [rootView.qh_commentButton setTitle:reviewTitle forState:UIControlStateNormal];
                
                [rootView.qh_tableView reloadData];
                
            } else {
                
                [self showToastWithText:message];
            }
            [rootView.qh_tableView.mj_header endRefreshing];
        } failure:^(NSError *error) {
            
            NSString *errorString = [NSString stringWithFormat:@"网络出错:%@,code:%ld", error.domain, (long)error.code];
            [self showToastWithText:errorString];
            [rootView.qh_tableView.mj_header endRefreshing];
        }];
    }];
    
    rootView.qh_tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        
        page++;
        [parameters setObject:[NSNumber numberWithInt:page] forKey:@"page"];
        
        [GQHCommentModel qh_fetchCommentsWithParameters:parameters success:^(BOOL status, NSNumber *code, NSString *message, NSArray<GQHCommentModel *> *comments) {
            
            if (status) {
                
                if (comments && comments.count > 0) {
                    
                    [commentModelArray addObjectsFromArray:comments];
                    rootView.qh_commentModelArray = commentModelArray;
                    [rootView.qh_tableView reloadData];
                    [rootView.qh_tableView.mj_footer endRefreshing];
                } else {
                    
                    [rootView.qh_tableView.mj_footer endRefreshingWithNoMoreData];
                }
            } else {
                
                [self showToastWithText:message];
                [rootView.qh_tableView.mj_footer endRefreshing];
            }
        } failure:^(NSError *error) {
            
            NSString *errorString = [NSString stringWithFormat:@"网络出错:%@,code:%ld", error.domain, (long)error.code];
            [self showToastWithText:errorString];
            [rootView.qh_tableView.mj_footer endRefreshing];
        }];
    }];
}

#pragma mark --Setter

#pragma mark --Getter

#pragma mark --View

@end
